﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using DTO;

namespace DataAccessTier
{
    public class ChiTietHangCCDAO:DBConnection
    {
        public ChiTietHangCCDAO() : base() { }

        public DataTable GetAllHangCC(string mancc)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "SELECT * FROM CHITIETHANGCC WHERE MANCC = '" + mancc + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                return dt;
            }
            catch (Exception)
            {
                conn.Close();
            }
            return null;
        }

        public DataTable GetAllMaHang()
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "SELECT * FROM HANGHOA";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                return dt;
            }
            catch (Exception)
            {
                conn.Close();
            }
            return null;
        }

        //Lấy hàng cung cấp theo mã hàng
        public DataSet GetTenHangCCTheoMa(string mahang)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("SELECT TENHANG FROM HANGHOA WHERE MAHANG='" + mahang + "'", conn);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                conn.Close();
                return dataSet;
            }
            catch (Exception)
            {
                conn.Close();
            }
            return null;
        }

        public bool Insert(string macthcc, string mancc, string mahang, string tenhangcc)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "INSERT INTO CHITIETHANGCC(MACTHCC,MANCC,MAHANG,TENHANGCC) VALUES('" + macthcc + "','" + mancc + "','" + mahang + "',N'" + tenhangcc + "')";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }

        public bool kiemtratontai(string macthcc)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            bool tatkt = false;
            SqlDataAdapter da_kiemtra = new SqlDataAdapter("SELECT * FROM CHITIETHANGCC WHERE MACTHCC = '" + macthcc + "'", conn);
            DataTable dt_kiemtra = new DataTable();
            da_kiemtra.Fill(dt_kiemtra);
            if (dt_kiemtra.Rows.Count > 0)
            {
                tatkt = true;
            }
            da_kiemtra.Dispose();
            conn.Close();
            return tatkt;
        }

        public bool Delete(string macthcc)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "DELETE FROM CHITIETHANGCC WHERE MACTHCC = '" + macthcc + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }

        public DataTable RandomMaKhachHang()
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "SELECT TOP 1 MACTHCC FROM CHITIETHANGCC ORDER BY MACTHCC DESC";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable datatable = new DataTable();
                da.Fill(datatable);
                conn.Close();
                return datatable;
            }
            catch(Exception)
            {
                conn.Close();
            }
            return null;
        }
    }
}
